package com.yw.smartcity_study;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yw.smartcity_study.api.AllServiceRes;

import java.util.ArrayList;
import java.util.List;

public class ServicesAdapter extends BaseAdapter {

    private List<AllServiceRes.RowsBean> datas = new ArrayList<>();

    public void setDatas(List<AllServiceRes.RowsBean> datas) {
        this.datas = datas;
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

//        View view;
//        ViewHolder viewHolder;
//        if(convertView == null){
//            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_service_item, parent, false);
//            viewHolder = new ViewHolder();
//            viewHolder.iconIv = view.findViewById(R.id.serviceIv);
//            viewHolder.nameTv = view.findViewById(R.id.nameTv);
//            view.setTag(viewHolder);
//        }else{
//            view = convertView;
//            viewHolder = (ViewHolder) view.getTag();
//        }

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_service_item, parent, false);
        TextView nameTv = view.findViewById(R.id.nameTv);
        ImageView serviceIv = view.findViewById(R.id.serviceIv);

        AllServiceRes.RowsBean data = datas.get(position);
        nameTv.setText(data.getServiceName());
        //显示图片
        if(position == datas.size()-1){
            serviceIv.setImageResource(R.drawable.services);
        }else{
            Glide.with(parent.getContext()).load(App.baseUrl + data.getImgUrl()).into(serviceIv);
        }

        return view;
    }

//    private class ViewHolder{
//        public ImageView iconIv;
//        public TextView nameTv;
//    }
}
